// packageG/mycomponent/supplyChain/sachertorte/sachertorte.js
var app = getApp();
let location = require("../../../../mybehaviors/location/location");
Component({
  behaviors: [location],
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true, // 复数插槽: 是
    addGlobalClass: true,
  },
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    btnFlag: {
      type: null
    },
    U_tabcontrol: {
      type: null,
    },
    isLast: {
      type: null,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isLoadMore: true, //true 可以加载更多
    isLoadMoreSet:false,
    page: 1,
    total_page: 0,
    arrIndex: 1, //组件加载下一页
    per_page:15, //每页多少条数据
    cakeCity:'',
    recommandList:[],
    imgPath: app.globalData.imgPath,
  },
  observers: {
    datas: function (data) {
      if (this.data.totalPage <= this.data.page) {
        this.setData({
          isLoadMore: false,
          allLoaded: true,
        });
      }
    },
    bottomShowBol: function (flag) {
      if (this.data.U_tabcontrol) {
        if(flag && this.data.isLast && this.data.isLoadMore) {
          this.getMoreData();
        }
      }
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
  lifetimes: {
    created() {
    },
    attached: function () {
      this.setListData('recommandList');
      this.frontendIndex();
    }
  },
  onReady() {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        let city = mapdata.address_component.city || '';
        if(!this.data.cakeCity) {
          this.data.cakeCity = city;
        }
        this.setData({
          point:{
            lng:e.lng,
            lat:e.lat
          }
        })
        this.frontendIndex();
      });
    } catch (e) {
      this.frontendIndex();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    classifyNav() {
      wx.navigateTo({
        url: `/packageF/sachertorte/sachertorteBand/sachertorteBand?city=${this.data.cakeCity}`
      })
    },
    setListData(setList) { //处理列表数据是自动获取还是手动获取
      let dataList = this.data.datas.list.data;
      if (dataList) {
        this.setData({
          total_page: dataList.last_page,
        });
        if (this.data.total_page <= this.data.page) {
          this.setData({
            isLoadMore: false,
          });
        }
        if(dataList.length >= this.data.datas.get_number) { //大于后台控制的数量就显示后台数量列表
          dataList.splice(this.data.datas.get_number,dataList.length-this.data.datas.get_number);
          this.setData({
            isLoadMore:false
          })
        } 
        this.setData({
          [setList]: dataList,
        });
      } else {
        if (this.data.datas.list.length <= 0) {
          this.setData({
            isLoadMore: false,
          });
        }
        this.setData({
          [setList]: this.data.datas.list,
        });
      }
    },
    toBand(e){
      let id = e.currentTarget.dataset.item.id;
      wx.navigateTo({
        url: `/packageF/sachertorte/sachertorteBandShop/sachertorteBandShop?brand_id=${id}&city=${this.data.city}`,
      })
    },
    goodsNav(event) {
      let id = event.currentTarget.dataset.id || '';
      wx.navigateTo({
        url:`/packageF/sachertorte/sachertorteGoods/sachertorteGoods?id=${id}&city=${this.data.cakeCity}`
      })
    },
    getNumber() { //处理获得定位控制自动获取数量
      if(this.data.datas.get_style == "1") {
        if(this.data.recommandList.length >= this.data.datas.get_number) { //大于后台控制的数量就显示后台数量列表
          this.data.recommandList.splice(this.data.datas.get_number,this.data.recommandList.length-this.data.datas.get_number);
          this.setData({
            recommandList:this.data.recommandList,
            isLoadMore:false
          })
        }
      }
    },
    frontendIndex() {
      let query = {
        decorate_id:this.data.page_id,
        component_id:this.data.component_id,
        component_key:'U_yzSupplyCake',
        city:this.data.cakeCity,
        lng:this.data.point.lng,
        lat:this.data.point.lat
      }
      let urlStr = app.getNetAddresss("home-page.get-decorate-page");
      app._postNetWork({
        url: urlStr,
        data:query,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.totalPage = res.data.last_page;
            if (!this.data.totalPage) {
              this.data.totalPage = 0;
            }
            if (this.data.totalPage <= this.data.page) {
              this.data.isLoadMore = false;
            }
            this.setData({
              recommandList: res.data.data,
              isLoadMore:this.data.isLoadMore
            })
            
            this.getNumber();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    getMoreData() {
      this.setData({
        isLoadMore:false
      })
      if (this.data.page >= this.data.totalPage) {
        this.setData({
          isLoadMore:false
        })
        return;
      } else {
        this.data.page = this.data.page + 1;
        let json = {
          decorate_id:this.data.page_id,
          component_id:this.data.component_id,
          component_key:'U_yzSupplyCake',
          page:this.data.page,
          city:this.data.cakeCity,
          lng:this.data.point.lng,
          lat:this.data.point.lat
        }
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        app._postNetWork({
          url: urlStr,
          data:json,
          success:  (resdata) => {
            let res = resdata.data;
            let list = res.data.data;
            if (res.result == 1) {
              this.data.totalPage <= this.data.page?this.data.isLoadMore = false:this.data.isLoadMore =true;
              this.setData({
                recommandList : this.data.recommandList.concat(list),
                isLoadMore:this.data.isLoadMore,
              })
              this.getNumber();
            } else {
              this.setData({
                isLoadMore:false
              })
              this.data.page = this.data.page - 1;
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  }
})
